package com.jufanshare.myblog.common.dto.auth;

/**
 * 作用：qq登录返回的参数
 * 作者：Tiddler
 * 时间：2018-08-2018/8/20 12：11
 * 类名：QQAuth
 **/
public class QQAuth {
    private String code;//Code
    private String access_token;//授权令牌，Access_Token。
    private String expires_in;//该access token的有效期，单位为秒。
    private String refresh_token;//在授权自动续期步骤中，获取新的Access_Token时需要提供的参数。
    private String openid;

    public QQAuth(String paramsStr) {
        if(paramsStr.indexOf("callback(")==0){
            int openid = paramsStr.indexOf("openid\":\"");
            String substring = paramsStr.substring(openid, paramsStr.length());
            String openVal = substring.split(":")[1];
            openVal=openVal.substring(0,openVal.indexOf("}")).replaceAll("\"","");
            this.openid=openVal;
        }else {
            String[] split = paramsStr.split("&");
            for (String s : split) {
                String paraName = s.split("=")[0];
                String paraVal=s.split("=")[1];
                if("access_token".equals(paraName)){
                    this.access_token=paraVal;
                }else if("expires_in".equals(paraName)){
                    this.expires_in=paraVal;
                }else if("refresh_token".equals(paraName)){
                    this.refresh_token=paraVal;
                }
            }
        }
    }
    public QQAuth() {
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getExpires_in() {
        return expires_in;
    }

    public void setExpires_in(String expires_in) {
        this.expires_in = expires_in;
    }

    public String getRefresh_token() {
        return refresh_token;
    }

    public void setRefresh_token(String refresh_token) {
        this.refresh_token = refresh_token;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    @Override
    public String toString() {
        return "QQAuth{" +
                "code='" + code + '\'' +
                ", access_token='" + access_token + '\'' +
                ", expires_in='" + expires_in + '\'' +
                ", refresh_token='" + refresh_token + '\'' +
                ", openid='" + openid + '\'' +
                '}';
    }
}
